﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_qlcontest : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            lb.Text = "";
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99ffcc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("id");
        Label lang = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lang");
        //Response.Write(id.Text);
        Response.Redirect("Default.aspx?page=edcontest&id=" + id.Text+"&lang="+lang.Text);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("insert into contest values(@id,@lang,@menu,@title,@des,@cont)", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter menu = new SqlParameter("@menu", SqlDbType.NVarChar, 50);
        menu.Value = me.Text;

        SqlParameter tit = new SqlParameter("@title", SqlDbType.NVarChar, 100);
        tit.Value = title.Text;

        SqlParameter cont = new SqlParameter("@cont", SqlDbType.NVarChar);
        cont.Value = nd.Value;

        SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 150);
        desc.Value = des.Value;

        SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
        Double idt = Common.ToSeconds(DateTime.Now);
        id.Value = idt;

        SqlParameter lang = new SqlParameter("@lang", SqlDbType.NVarChar, 2);
        lang.Value = lg.SelectedValue;

        mySqlSelect.Parameters.Add(menu);
        mySqlSelect.Parameters.Add(tit);
        mySqlSelect.Parameters.Add(cont);
        mySqlSelect.Parameters.Add(desc);
        mySqlSelect.Parameters.Add(id);
        mySqlSelect.Parameters.Add(lang);
        mySqlSelect.ExecuteNonQuery();
        cn.Close();
        lb.Visible = true;
        lb.Text = "Bạn đã cập nhật thành công";
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("id");
        Label lang = (Label)this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("lang");
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("delete from contest where id = @id and lang = @lang", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter idt = new SqlParameter("@id", SqlDbType.BigInt);
        idt.Value = Int64.Parse(id.Text);

        SqlParameter langt = new SqlParameter("@lang", SqlDbType.NVarChar, 2);
        langt.Value = lang.Text;

        mySqlSelect.Parameters.Add(idt);
        mySqlSelect.Parameters.Add(langt);
        mySqlSelect.ExecuteNonQuery();
        cn.Close();
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }
}